package lanou.giftsay_01.fragment.main;

import android.content.Intent;
import android.os.Handler;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;
import android.widget.Toast;

import java.util.ArrayList;

import lanou.giftsay_01.adapter.hotRvAdapter;
import lanou.giftsay_01.bean.HotBean;
import lanou.giftsay_01.fragment.mine.OnItemClickListener;
import lanou.giftsay_01.main.guidemain.SearchMainActivity;
import lanou.giftsay_01.main.hotmain.HotSecondActivity;
import lanou.giftsay_01.tools.BaseFragment;
import lanou.giftsay_01.tools.NetTool;
import lanou.giftsay_01.tools.URLValues;
import lanou.giftsay_01.R;


/**
 * Created by dllo on 16/8/15.
 */
public class HotFragment extends BaseFragment implements SwipeRefreshLayout.OnRefreshListener {

    private RecyclerView hotRv;
    private hotRvAdapter adapter;
    private ImageView iv;
    private SwipeRefreshLayout swipeRefreshLayout;
    private NetTool netTool;

    @Override
    protected int setLayout() {
        return R.layout.hot_fragment;
    }

    @Override
    protected void initView() {
        iv = (ImageView) getView().findViewById(R.id.search_category);
        swipeRefreshLayout = (SwipeRefreshLayout) getView().findViewById(R.id.hot_sr);
        hotRv = (RecyclerView) getView().findViewById(R.id.hot_rv);
        GridLayoutManager manager = new GridLayoutManager(mContext ,2);
        hotRv.setLayoutManager(manager);
        adapter = new hotRvAdapter(mContext);
    }
    @Override
    protected void initData() {
        swipeRefreshLayout.setColorSchemeResources(android.R.color.holo_blue_bright);   // 设置下拉圆圈上的颜色，蓝色
        swipeRefreshLayout.setSize(SwipeRefreshLayout.LARGE);// 设置圆圈的大小
        swipeRefreshLayout.setDistanceToTriggerSync(400);// 设置手指在屏幕下拉多少距离会触发下拉刷新
        swipeRefreshLayout.setOnRefreshListener(this);
        netTool = new NetTool();
        netTool.getNetData(URLValues.HOT_SPOT, HotBean.class, new NetTool.NetListener<HotBean>() {
            @Override
            public void onSuccess(HotBean hotBean) {
                ArrayList<HotBean> arrayList = new ArrayList<HotBean>();
                arrayList.add(hotBean);
                adapter.setArrayList(arrayList);
                hotRv.setAdapter(adapter);
            }

            @Override
            public void onError(String errorMsg) {

            }
        });


        adapter.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void OnClick(int position, ArrayList<HotBean> arrayList) {
                Intent intent = new Intent(HotFragment.this.getContext() , HotSecondActivity.class);
                intent.putExtra("key",arrayList.get(0).getData().getItems().get(position).getData().getUrl());
                intent.putExtra("url",arrayList.get(0).getData().getItems().get(position).getData().getCover_image_url());
                intent.putExtra("price" , arrayList.get(0).getData().getItems().get(position).getData().getPrice());
                intent.putExtra("counts" ,String.valueOf(arrayList.get(0).getData().getItems().get(position).getData().getFavorites_count()));
                intent.putExtra("name" ,arrayList.get(0).getData().getItems().get(position).getData().getName());
                startActivity(intent);
            }
        });
        iv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent( HotFragment.this.getContext() , SearchMainActivity.class);
                startActivity(intent);
            }
        });
    }

    @Override
    public void onRefresh() {
        netTool.getNetData(URLValues.HOT_SPOT, HotBean.class, new NetTool.NetListener<HotBean>() {
            @Override
            public void onSuccess(HotBean hotBean) {
                ArrayList<HotBean> arrayList = new ArrayList<HotBean>();
                arrayList.add(hotBean);
                adapter.setArrayList(arrayList);
                hotRv.setAdapter(adapter);
                new Handler().postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        // 停止刷新
                        swipeRefreshLayout.setRefreshing(false);
                        Toast.makeText(mContext, "刷新成功", Toast.LENGTH_SHORT).show();
                    }
                }, 5); // 5秒后发送消息，停止刷新
            }

            @Override
            public void onError(String errorMsg) {

            }
        });



    }
}
